package com.ming.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ming.reggie.common.R;
import com.ming.reggie.entity.Employee;
import com.ming.reggie.entity.Page;
import com.ming.reggie.exception.BusinessException;
import com.ming.reggie.mapper.EmployeeMapper;
import com.ming.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ming
 */
@Service
public class EmployeeServiceImpl  implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    /**
     * 登录的业务方法
     */
    @Override
    public R<Employee> login(Employee employee) {
        //数据库查出的用户
        Employee loginEmployee = employeeMapper.login(employee);

        //1.判断用户是否存在
        if(loginEmployee == null){
            throw  new BusinessException("用户名或密码错误");
        }

        //2.判断密码是否相同
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if(!loginEmployee.getPassword().equals(password)){
            throw new BusinessException("用户名或密码错误");
        }

        //3.判断状态是否禁用
        if(loginEmployee.getStatus() == 0){
            throw new BusinessException("该账户已被禁用,请联系管理员");
        }

        return R.success(loginEmployee);
    }

    /**
     * 新增员工的业务方法
     * @param employee
     * @return
     */
    @Override
    public boolean InsertEmployee(Employee employee) {
        //完善数据
        //判断账户是否唯一
        if(employeeMapper.login(employee) != null){
            //代表账号重复
            throw new BusinessException("账号已存在,请重新更改");
        }
        //默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //启用状态
        employee.setStatus(1);
        //创建时间
        employee.setCreateTime(LocalDateTime.now());
        //更新时间
        employee.setUpdateTime(LocalDateTime.now());
        Integer insert = employeeMapper.Insert(employee);
        return insert>0;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Employee> findByPage(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);

        List<Employee> employeeList = employeeMapper.findByPage(name);

        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);
        Page<Employee> employeePage = new Page<>();
        employeePage.setRecords(pageInfo.getList());
        employeePage.setTotal(pageInfo.getTotal());
        return employeePage;
    }

    /**
     * 根据id查找员工
     *
     * @param id
     * @return
     */
    @Override
    public Employee findById(Long id) {
        Employee employee = employeeMapper.findById(id);
        return employee;
    }

    /**
     * 修改员工
     *
     * @param employee
     * @return
     */
    @Override
    public Integer update(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        Integer update = employeeMapper.update(employee);
        return update;
    }
}
